#include <Misc.au3>
#include <ScreenCapture.au3>
#include <..\utility.au3>

HotKeySet("{PAUSE}", "theExit")
HotKeySet("9", "theExit")

call("screenshot", "start")

$numsweeps = IniRead("settings.ini", "run", "numsweeps", 1)

$stormarmor = 1 ;storm armor is slot 1
$explosion = 1 ;archon explosion is slot 1
$teleport = 2 ;teleport is slot 2
$magicweapon = 3 ;magic weapon is slot 3
$familiar = 4 ;familiar is slot 4
$goldpiles = 5 ;Number of gold piles to pick up

$chestnum = 0;how many chests found
$chestlegnum = 0;how many legendaries we found in chest


While 1 
	processkey('70', "mousePos") ;F1
	processkey('71', "leorics") ;F2
	processkey('72', "getLegendaries") ;F3
	processkey('73', "repair") ;F4
	processkey('74', "findring") ;F5
	processkey('75', "findcolor") ;F6
	processkey('76', "checkdead") ;F7
	processkey('77', "castSpelltest") ;F8
	processkey('78', "checkdisco") ;F9
	processkey('79', "getGems") ;F10
WEnd

Func leorics()	
While 1
	Sleep($lagfactor)
	
	;Log Run Start
	call("logmessage", "Starting Run " & $runs)
	$runs = $runs + 1
	
	;Randomly take a break
	If $runs > 10 Then
		call("takebreak")
	EndIf
	
	;Repair if needed
	call("repair")
	
	;Move to portal
	call("fuzzyclick","left", 113, 24)
	
	Local $mode = Random(1,6,1)
	If $mode = 1 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 2 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	ElseIf $mode = 3 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 4 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	ElseIf $mode = 5 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	Else
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	EndIf
	
	
	call("fuzzysleep",4600 * $runspeed)
	Sleep($lagfactor)
	
	
	
	call("fuzzymove", 1393,667)
	Sleep(100)
	$colortest = PixelGetColor(1384,606)
	
	;call("fuzzymove", 1532,749)
	;Sleep(100)
	;$colortest2 = PixelGetColor(1550,687)
	
	;call("fuzzymove", 1548,758)
	;Sleep(100)
	;$colortest3 = PixelGetColor(1549,686)
	
	if($colortest = 16777215) Then
		call("fuzzyclick","left",1393,667)
		call("fuzzysleep", Random(2200,2400,1))
		
		call("screenshot", "Chest")
		call("getLegendaries", 0)
		
		$chestnum = $chestnum+1
		call("logmessage", "Chest " & $chestnum)
		
		If ($statusLegendaryDrop = 1) Then
			$chestlegnum = $chestlegnum + 1
			call("logmessage", "Chest Legendary "& $chestlegnum)
		EndIf
		
		call("fuzzyclick","left",512,112)
		call("fuzzysleep", Random(1000,1200,1))
		
		;Find door
		;Local $coord = PixelSearch(300, 0, 600, 100, 0x285EC5, 20)

		;If Not @error Then
		;	MouseClick("left", $coord[0], $coord[1]+10)
		;	fuzzysleep(2000 * $runspeed)
		;EndIf
	;ElseIf ($colortest2 = 16777215) Then
	;	call("fuzzyclick","left",1532,749)
	;	call("fuzzysleep", Random(1000,1200,1))
		
	;	call("getLegendaries", 0)
		;MsgBox(0,"ss","ss")
		
	;	call("fuzzyclick","left",528,137)
	;	call("fuzzysleep", Random(1000,1200,1))
	;ElseIf ($colortest3 = 16777215) Then
	;	call("fuzzyclick","left",1548,758)
	;	call("fuzzysleep", Random(1000,1200,1))
		
	;	call("getLegendaries", 0)
	;	MsgBox(0,"ss","ss")
		
	;	call("fuzzyclick","left",528,137)
	;	call("fuzzysleep", Random(1000,1200,1))
	Else
		;WORKING CLICK
		call("fuzzyclick","left",697,243)
	EndIf
	
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;call("fuzzyclick","left",1421,704)
	;call("fuzzysleep", Random(1000,1200))
	
	;Pick up Legendaries
	;call("getLegendaries", 0)
	;call("getGems", 0)
	
	;Wait for cooldown
	;Sleep(Random(1400,1500))
	
	
	
	
	;Find door
	;Local $coord = PixelSearch(300, 0, 600, 100, 0x285EC5, 20)

	;If Not @error Then
	;	MouseClick("left", $coord[0], $coord[1]+10)
	;	fuzzysleep(2000 * $runspeed)
	;EndIf
	
	call("fuzzysleep", Random(800,1000,1))
	
	call("fuzzymove", 1023, 200)
	Sleep(Random(1000,1200,1))
	
	Send("2")
	Sleep(Random(1000,1400,1))
	
	;Fire forward
	MouseDown("right")
	call("castSpell", $explosion)
	call("fuzzymove", 1522, 200)
	Sleep(Random(2000,2200,1))
	call("castSpell", $explosion)
	call("fuzzymove", 1522, 200)
	Sleep(Random(2000,2200,1))
	call("castSpell", $explosion)
	call("fuzzymove", 1522, 200)
	Sleep(Random(2000,2200,1))
	call("castSpell", $explosion)
	call("fuzzymove", 1522, 200)
	Sleep(Random(2000,2200,1))
	call("castSpell", $explosion)
	call("fuzzymove", 1522, 200)
	Sleep(Random(2000,2200,1))
	call("castSpell", $explosion)
	MouseUp("right")
	
	Sleep(Random(50,150,1))
	
	;Fire at remaining guys
	
	For $i=0 to 2
		If call("checkdead", 0) = 1 Then
			ExitLoop
		EndIf
		
		
		
		;Press 1 on third loop
		If $i = 3 Then
			Send("1")
		EndIf
		
		
		
		Local $finalmobs = PixelSearch(400,0,1400,700,0xF70000,15)
		if IsArray($finalmobs) Then
			MouseDown("right")
			call("fuzzymove", $finalmobs[0],$finalmobs[1]+200)
			call("fuzzysleep",Random(1800,2400,1))
			MouseUp("right")
			call("fuzzysleep", 200)
			call("castSpell", $explosion)
		Else
			ExitLoop
		EndIf
	Next
	
	
	call("fuzzysleep", Random(1400,1800,1))
	
	;Pick up Legendaries
	call("getLegendaries", 0)
	call("getGems", 0)
	
	;Check if we are dead and just exit
	If call("checkdead", 0) = 1 Then
		call("exitgame", 0)
		ContinueLoop
	EndIf
	
	;Pick up gold
	;call("pickupGold", 1)
	
	call("exitgame")
	
	;Restart Script
WEnd	
	
EndFunc

Func FindDoor()
	;Find door
	Local $coord = PixelSearch(300, 0, 600, 100, 0x285EC5, 20)

	If Not @error Then
		MouseClick("left", $coord[0], $coord[1])
		fuzzysleep(2000 * $runspeed)
	EndIf
EndFunc